/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilitario;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 *
 * @author Administrador
 */
public class CCalculos {

    public String calcularFormula(String ecuacion, String[] valores) {

        for (int i = 0; i < valores.length; i+=2) {
            if (Double.parseDouble(valores[i + 1]) >= 0.00) {
                ecuacion = ecuacion.replaceAll(valores[i], valores[i + 1]);
            }
        }
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        System.out.println(ecuacion + " = ");
        try {
            Object result = engine.eval(ecuacion);
            System.out.println(ecuacion + " = " + result);
            ecuacion = result.toString();
        } catch (ScriptException se) {
            se.printStackTrace();
        }
        return ecuacion;
    }
}
